package com.zhuiyun.educations.api.sysuserback.service.impl;

import com.zhuiyun.educations.api.sysUser.entiy.SysUser;
import com.zhuiyun.educations.api.sysuserback.entiy.DTO.SysUserBackUpdatePasswordDTO;
import com.zhuiyun.educations.api.sysuserback.entiy.SysUserBack;
import com.zhuiyun.educations.api.sysuserback.entiy.vo.SysUserBackVo;
import com.zhuiyun.educations.api.sysuserback.mapper.SysUserBackMapper;
import com.zhuiyun.educations.api.sysuserback.service.SysUserBackService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.GetUuid;
import com.zhuiyun.educations.util.Md5Utils;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

/**
 * @ClassName SysUserBackServiceImpl
 * @Description TODO
 * @Author MJL
 * @Date 2023/6/15 15:37
 **/
@Service
public class SysUserBackServiceImpl implements SysUserBackService {
    @Resource
    SysUserBackMapper sysUserBackMapper;

    /*/**
    * @Author MJL
    * @Description todo 登录
    * @Date 2023/6/15 16:06
    * @Return com.zhuiyun.educations.common.response.CommonResult * @param sysUserBack
    */
    @Override
    public CommonResult login(SysUserBack sysUserBack) {
        String userLoginName = sysUserBack.getUserLoginName();
        String userPassword = sysUserBack.getUserPassword();
        if (userLoginName.isEmpty() || userPassword.isEmpty()){
            return CommonResult.error(9999,"用户名和密码不能为空");
        }
        Example example = new Example(SysUserBack.class);
        example.createCriteria().andEqualTo("userLoginName",userLoginName);
        SysUserBack sysUserBack1 = sysUserBackMapper.selectOneByExample(example);
        if (sysUserBack1 == null){
            return CommonResult.error(9999,"账号或密码错误");
        }
        if (Md5Utils.getMd5(userPassword).equals(sysUserBack1.getUserPassword())){
            String token = GetUuid.getUuid();
            HashMap<String,String> map = new HashMap<>();
            map.put("token",token);
            map.put("userLoginName",userLoginName);
            map.put("id", String.valueOf(sysUserBack1.getId()));
            return CommonResult.ok(map);
        }else {
            return CommonResult.error(9999,"账号或密码错误");
        }
    }

    /*/**
    * @Author MJL
    * @Description todo 用户表全查
    * @Date 2023/6/15 20:14
    * @Return com.zhuiyun.educations.util.pageUtil.PageResult * @param sysUserBack
* @param pageRequest
    */
    @Override
    public PageResult userList(SysUserBackVo sysUserBackVo, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(sysUserBackMapper.userList(sysUserBackVo));
        return pageResult;
    }

    /**
    * @Author MJL
    * @Description todo 用户表添加
    * @Date 2023/6/16 9:08
    * @Return  * @param null
    */
    @Override
    public int SysUserAdd(SysUserBack sysUserBack) {
        // 获取当前时间
        Date date = new Date();
        // 添加时间时获取当前时间
        sysUserBack.setCreateTime(date);
        //添加用户时默认角色为普通员工（3）
        sysUserBack.setRoleId(3);
        // 添加时字段del_flag默认为0
        sysUserBack.setDelFlag(0);
        // 添加密码时为md5加密的
        String userPassword = sysUserBack.getUserPassword();
        sysUserBack.setUserPassword(Md5Utils.getMd5(userPassword));
        return sysUserBackMapper.SysUserAdd(sysUserBack);
    }

    /**
    * @Author MJL
    * @Description todo 用户表逻辑删除
    * @Date 2023/6/16 13:49
    * @Return  * @param null
    */
    @Override
    public int SysUserDelete(Integer id) {
        return sysUserBackMapper.SysUserDelete(id);
    }

    /**
    * @Author MJL
    * @Description todo 用户表修改
    * @Date 2023/6/16 14:39
    * @Return  * @param null
    */
    @Override
    public int SysUserUpdate(SysUserBack sysUserBack) {
        // 获取当前时间
        Date date = new Date();
        // 获取当前修改时间
        sysUserBack.setUpdateTime(date);
        return sysUserBackMapper.SysUserUpdate(sysUserBack);
    }

    /**
    * @Author MJL
    * @Description todo 修改密码
    * @Date 2023/6/30 8:58
    * @Return  * @param null
    */
    @Override
    public Integer SysUserUpdatePassword(SysUserBackUpdatePasswordDTO sysUserBackUpdatePasswordDTO) {
        // 修改密码时进行md5加密
        sysUserBackUpdatePasswordDTO.setUserPassword(Md5Utils.getMd5(sysUserBackUpdatePasswordDTO.getUserPassword()));
        return sysUserBackMapper.SysUserUpdatePassword(sysUserBackUpdatePasswordDTO);
    }

    /**
    * @Author MJL
    * @Description todo 根据用户名查询是否重复
    * @Date 2023/6/30 8:58
    * @Return  * @param null
    */
    @Override
    public CommonResult repeatUserLoginName(String userLoginName) {
        Example example = new Example(SysUserBack.class);
        example.createCriteria().andEqualTo("userLoginName",userLoginName)
                .andEqualTo("delFlag",0);
        SysUserBack sysUserBack = sysUserBackMapper.selectOneByExample(example);
        return CommonResult.ok(sysUserBack);
    }

    @Override
    public int userAssignmentRole(SysUserBack sysUserBack) {
        return sysUserBackMapper.userAssignmentRole(sysUserBack);
    }
}
